//
//  RGMeVipVC.m
//  RedGrass
//
//  Created by wuyine on 2018/12/12.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//
#import "RGMeVipVC.h"
#import "RGMeWalletInterfaceBuilder.h"
#import "RGRechargeModel.h"
@interface RGMeVipPriceCell ()
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *cheapPriceLabel;
@property (nonatomic,strong) UILabel *priceLabel;
@end
@implementation RGMeVipPriceCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.layer.cornerRadius = 8;
        self.contentView.layer.borderColor = [UIColor clearColor].CGColor;
        self.contentView.layer.borderWidth = 1;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addSubview:self.timeLabel];
        [self addSubview:self.cheapPriceLabel];
        [self addSubview:self.priceLabel];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.leading.mas_equalTo(15);
        }];
        
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(-15);
            make.centerY.mas_equalTo(0);
        }];
        
        [_cheapPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(_priceLabel.mas_leading).offset(-5);
            make.centerY.mas_equalTo(0);
        }];
    }
    return self;
}

- (void)setSelected:(BOOL)selected {
    if (selected) {
        self.contentView.layer.borderColor = UICOLOR_WITH_RGBINT(0xf73152).CGColor;
    }else {
        self.contentView.layer.borderColor = [UIColor clearColor].CGColor;
    }
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textColor = UICOLOR_WITH_RGBINT(0x333333);
        _timeLabel.font = FONT(customFontName, 18);
    }
    return _timeLabel;
}

- (UILabel *)cheapPriceLabel {
    if (!_cheapPriceLabel) {
        _cheapPriceLabel = [[UILabel alloc] init];
        _cheapPriceLabel.textColor = UICOLOR_WITH_RGBINT(0x999999);
        _cheapPriceLabel.font = FONT(customFontName, 13);
    }
    return _cheapPriceLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = UICOLOR_WITH_RGBINT(0xf73152);
        _priceLabel.font = FONT(customFontName, 24);
    }
    return _priceLabel;
}
@end


@interface RGMeVipVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIImageView *userIcon;
@property (nonatomic,strong) UILabel *userName;
@property (nonatomic,strong) UIImageView *vipIcon;
@property (nonatomic,strong) UILabel *vipStatus;
@property (nonatomic,strong) UIView *tipView;
@property (nonatomic,strong) UITableView *moneyTableView;
@property (nonatomic,strong) UIView *bottomView;
@property (nonatomic,strong) UILabel *reductionLabel;
@property (nonatomic,strong) UILabel *totalMoneyLabel;
@property (nonatomic,strong) NSIndexPath *lastIndexPath;
@property (nonatomic,strong) NSArray *dataList;
@end

@implementation RGMeVipVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showBackButton];
    self.customNavBar.title = @"开通会员";
    self.customContentView.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
    [self initUI];
    [self addMasonry];
    [self loadData];
}

- (void)loadData {
    HttpRequest *quest = [HttpRequest buildRequest:url_rechargeList];
    NSDictionary *params = [RGMeWalletInterfaceBuilder buildRechargeListWithOperationType:OpenMembership];
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf dismissLoading];
        weakSelf.dataList = [RGRechargeModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        [weakSelf.moneyTableView reloadData];
        [weakSelf tableView:weakSelf.moneyTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//        [weakSelf.moneyTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
//        weakSelf.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf showTextMessageWithMessage:message];
    }];
}

- (void)initUI {
    [self.customContentView addSubview:self.userIcon];
    [self.customContentView addSubview:self.userName];
    [self.customContentView addSubview:self.vipIcon];
    [self.customContentView addSubview:self.vipStatus];
    [self.customContentView addSubview:self.tipView];
    [self.customContentView addSubview:self.moneyTableView];
    [self.customContentView addSubview:self.bottomView];
}

- (void)addMasonry {
    [_userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(45, 45));
    }];
    
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_userIcon.mas_trailing).offset(8);
        make.top.equalTo(_userIcon.mas_top).offset(2);
    }];
    
    [_vipIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_userName.mas_trailing).offset(10);
        make.centerY.equalTo(_userName.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(11.5, 12));
    }];
    
    [_vipStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(_userName.mas_leading);
        make.bottom.equalTo(_userIcon.mas_bottom).offset(-2);
    }];
    
    [_tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_userIcon.mas_bottom).offset(40);
    }];
    
    [_moneyTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.top.equalTo(_tipView.mas_bottom).offset(10);
        make.height.mas_equalTo(63*4);
    }];
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(KIsiPhoneX?-34:0);
        make.height.mas_equalTo(55);
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RGMeVipPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"price"];
    RGRechargeModel *model = [self.dataList objectAtIndex:indexPath.row];
    cell.timeLabel.text = model.content;
    NSMutableAttributedString *cheapPrice = [[NSMutableAttributedString alloc] initWithString:model.originalPrice];
    [cheapPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, cheapPrice.length)];
    cell.cheapPriceLabel.attributedText = cheapPrice;
    cell.priceLabel.text = model.payPrice;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RGMeVipPriceCell *lastCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
    lastCell.contentView.layer.borderColor = [UIColor clearColor].CGColor;
    RGMeVipPriceCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.layer.borderColor = UICOLOR_WITH_RGBINT(0xf73152).CGColor;
    self.lastIndexPath = indexPath;
    RGRechargeModel *model = [self.dataList objectAtIndex:indexPath.row];
    _reductionLabel.text = [NSString stringWithFormat:@"￥%@",model.payPrice];
    NSMutableAttributedString *cheapPrice = [[NSMutableAttributedString alloc] initWithString:model.originalPrice];
    [cheapPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, cheapPrice.length)];
    _totalMoneyLabel.attributedText = cheapPrice;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 63;
}

- (UIImageView *)userIcon {
    if (!_userIcon) {
        _userIcon = [[UIImageView alloc] init];
        [_userIcon setImageWithURL:[NSURL URLWithString:[RGUserInfoModel getUserInfo].image] placeholder:IMAGE_NAMED(@"会员_头像")];
    }
    return _userIcon;
}

- (UILabel *)userName {
    if (!_userName) {
        _userName = [[UILabel alloc] init];
        _userName.text = [RGUserInfoModel getUserInfo].nickname;
        _userName.font = FONT(customFontName, 14);
        _userName.textColor = UICOLOR_WITH_RGBINT(0xffffff);
    }
    return _userName;
}

- (UIImageView *)vipIcon {
    if (!_vipIcon) {
        _vipIcon = [[UIImageView alloc] init];
        _vipIcon.image = IMAGE_NAMED(@"会员_未开通");
    }
    return _vipIcon;
}

- (UILabel *)vipStatus {
    if (!_vipStatus) {
        _vipStatus = [[UILabel alloc] init];
        _vipStatus.textColor = UICOLOR_WITH_RGBINT(0xeeeeee);
        _vipStatus.text = @"当前未开通VIP";
        _vipStatus.font = FONT(customFontName, 13);
    }
    return _vipStatus;
}

- (UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] init];
        UIImageView *vipIcon = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"会员_会员")];
        [_tipView addSubview:vipIcon];
        [vipIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.bottom.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(18.5, 19));
        }];
        
        UILabel *tip = [[UILabel alloc] init];
        tip.text = @"会员可以拍摄发布60秒以内的短视频";
        tip.font = FONT(customFontName, 14);
        tip.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        [_tipView addSubview:tip];
        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(vipIcon.mas_trailing).offset(8);
            make.centerY.equalTo(vipIcon.mas_centerY);
            make.trailing.mas_equalTo(0);
        }];
    }
    return _tipView;
}

- (UITableView *)moneyTableView {
    if (!_moneyTableView) {
        _moneyTableView = [[UITableView alloc] init];
        [_moneyTableView registerClass:[RGMeVipPriceCell class] forCellReuseIdentifier:@"price"];
        _moneyTableView.tableFooterView = [UIView new];
        _moneyTableView.delegate = self;
        _moneyTableView.dataSource = self;
        _moneyTableView.layer.cornerRadius = 8;
    }
    return _moneyTableView;
}

- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.text = @"总计：";
        tipLabel.textColor = UICOLOR_WITH_RGBINT(0x333333);
        tipLabel.font = FONT(customFontName, 17);
        [_bottomView addSubview:tipLabel];
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(15);
            make.centerY.mas_equalTo(0);
        }];
        
        [_bottomView addSubview:self.reductionLabel];
        [_bottomView addSubview:self.totalMoneyLabel];
        [_reductionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(tipLabel.mas_trailing).offset(1);
            make.centerY.mas_equalTo(0);
        }];
        [_totalMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(_reductionLabel.mas_trailing).offset(10);
            make.centerY.mas_equalTo(0);
        }];
        
        UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        payBtn.backgroundColor = UICOLOR_WITH_RGBINT(0xf73152);
        [payBtn setTitle:@"确认支付" forState:UIControlStateNormal];
        payBtn.titleLabel.textColor = [UIColor whiteColor];
        payBtn.titleLabel.font = FONT(customFontName, 17);
        [payBtn addTarget:self action:@selector(pay:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:payBtn];
        [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(125, 55));
            make.trailing.top.mas_equalTo(0);
        }];
    }
    return _bottomView;
}

- (UILabel *)reductionLabel {
    if (!_reductionLabel) {
        _reductionLabel = [[UILabel alloc] init];
        _reductionLabel.text = @"";
        _reductionLabel.textColor = UICOLOR_WITH_RGBINT(0x333333);
        _reductionLabel.font = FONT(customFontName, 17);
    }
    return _reductionLabel;
}

- (UILabel *)totalMoneyLabel {
    if (!_totalMoneyLabel) {
        _totalMoneyLabel = [[UILabel alloc] init];
        _totalMoneyLabel.text = @"";
        _totalMoneyLabel.textColor = UICOLOR_WITH_RGBINT(0x999999);
        _totalMoneyLabel.font = FONT(customFontName, 14);
    }
    return _totalMoneyLabel;
}
@end
